package com.oceansoft.mobile.econsole.modules.movecar.controller;

import com.oceansoft.mobile.econsole.modules.base.controller.BaseController;
import com.oceansoft.mobile.econsole.modules.base.entity.Pager;
import com.oceansoft.mobile.econsole.modules.base.entity.Query;
import com.oceansoft.mobile.econsole.modules.base.entity.Result;
import com.oceansoft.mobile.econsole.modules.movecar.entity.MoveCar;
import com.oceansoft.mobile.econsole.modules.movecar.service.IMoveCarService;
import org.apache.poi.hssf.usermodel.*;
import org.omg.CORBA.Request;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.text.SimpleDateFormat;

import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

/**
 * Created by Administrator on 2016/2/15.
 */
@Controller
@RequestMapping("movecar")
public class MoveCarController extends BaseController{

    @Resource
    private IMoveCarService moveCarService;

    /**
     * 堵路移车列表
     * @param query
     * @param model
     * @return
     */
    @RequestMapping(value = "index")
    public String gotoMoveCarIndex(Query query, Model model){
        Pager<MoveCar> pager = moveCarService.loadAllMoveCarsByPage(query);
        model.addAttribute("pager",pager);
        model.addAttribute("query",query);
        log("查看堵路移车列表");
        return "movecar/move-car-list";
    }

    @RequestMapping(value = "list",method = POST)
    public String loadPageByCondition(Query query, Model model){
        Pager<MoveCar> pager = moveCarService.loadAllMoveCarsByPage(query);
        model.addAttribute("pager",pager);
        model.addAttribute("query",query);
        log(String.format("查看堵路移车第%dÎ页", query.getPageNum()));
        return "movecar/move-car-list";
    }

    @RequestMapping(value = "viewMoveCar/{movecar_id}",method = GET)
    public String viewMoveCar(@PathVariable("movecar_id")String movecar_id,Model model){
        MoveCar moveCar = (MoveCar) moveCarService.loadMoveCarById(movecar_id).getData();
        model.addAttribute("movecar",moveCar);
        model.addAttribute("handleName",getSession().getRealName());
        return "movecar/move-car-details";
    }

    @ResponseBody
    @RequestMapping(value = "editMoveCar",method = POST)
    public Result editMoveCar(HttpServletRequest request){
        String id = request.getParameter("id");
        String handleStatus = request.getParameter("handleStatus");
        String handleResult = request.getParameter("handleResult");
        String handleName = request.getParameter("handleName");
        MoveCar moveCar = new MoveCar();
        moveCar.setId(id);
        moveCar.setHandleStatus(handleStatus);
        moveCar.setHandleResult(handleResult);
        moveCar.setHandleName(handleName);
        return moveCarService.editMoveCar(moveCar);
    }

    @RequestMapping(value = "exportExcel.xls",method = GET)
    public void exportExcel(HttpServletResponse response,Query query){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        // 第一步，创建一个webbook，对应一个Excel文件
        HSSFWorkbook wb = new HSSFWorkbook();
        // 第二步，在webbook中添加一个sheet,对应Excel文件中的sheet
        HSSFSheet sheet = wb.createSheet("内容反馈");
        // 第三步，在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
        HSSFRow row = sheet.createRow((int) 0);
        // 第四步，创建单元格，并设置值表头 设置表头居中
        HSSFCellStyle style = wb.createCellStyle();
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式

        HSSFCell cell = row.createCell((short)0);
        cell.setCellValue("序号");
        cell.setCellStyle(style);
        cell = row.createCell((short)1);
        cell.setCellValue("登陆账号");
        cell.setCellStyle(style);
        cell = row.createCell((short)2);
        cell.setCellValue("联系方式");
        cell.setCellStyle(style);
        cell = row.createCell((short)3);
        cell.setCellValue("反馈时间");
        cell.setCellStyle(style);
        cell = row.createCell((short)4);
        cell.setCellValue("手机机型");
        cell.setCellStyle(style);
        cell = row.createCell((short)4);
        cell.setCellValue("反馈意见");
        cell.setCellStyle(style);
    }

}
